package tests;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.util.Set;

/**
 * 抽奖测试
 */
public class LotteryPage extends Utils {

    private static String url = "http://82.156.185.199:8080/admin.html";

    public LotteryPage() {
        super(url);
    }

    /**
     * 页面元素
     */
    public void testPage() throws InterruptedException {
        // 抽奖页面1
        driver.switchTo().frame(0);
        String expectText = "活动进行中，去抽奖";
        String actualText = driver.findElement(By.cssSelector("#activities > div:nth-child(1) > div.active > p > a")).getText();
        System.out.println(actualText);
        Assertions.assertEquals(expectText, actualText);
        driver.findElement(By.cssSelector("#activities > div:nth-child(1) > div.active > p > a")).click();
        // 切换句柄
        Thread.sleep(3000);
        String curWindow = driver.getWindowHandle();
        Set<String> allWindows = driver.getWindowHandles();
        for(String w : allWindows){
            if(w != curWindow){
                driver.switchTo().window(w);
            }
        }
        // 测试名称
        driver.findElement(By.cssSelector("#title"));
        // 描述
        driver.findElement(By.cssSelector("#disc"));
        // 图片
        driver.findElement(By.cssSelector("#image"));
        // "查看上一奖项" 按钮
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.pre-btn"));
        // "开始抽奖" 按钮
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn"));
    }


    /**
     * 抽奖（两次）
     */
    public void LotteryPage(){
        // 第一次
        // 点击 "开始抽奖"
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();
        // 点击 "点我确定"
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();
        // 点击 "已抽完，下一步"
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();

        // 第二次
        // 点击 "开始确定"
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();
        // 点击 "点我确定"
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();
        // 点击 "已抽完，下一步"
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();

        // "已全部抽完"
        String expectText2 = "已全部抽完";
        String actualText2 = driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).getText();
        Assertions.assertEquals(expectText2, actualText2);

        // 点击 "分享结果" 按钮
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.copy-btn")).click();
        // 处理弹窗
        Alert alert = driver.switchTo().alert();
        String expectText3 = "链接已复制到剪贴板";
        String actualText3 = alert.getText();
        Assertions.assertEquals(expectText3, actualText3);
        alert.accept();

        // 返回活动列表页，查看活动状态
        driver.get(url);
        driver.switchTo().frame(0);
        String expectText1 = "活动已完成，查看中奖名单";
        String actualText1 = driver.findElement(By.cssSelector("#activities > div:nth-child(1) > div.inactive > p > a")).getText();
        Assertions.assertEquals(expectText1, actualText1);

        // 退出
        driver.switchTo().defaultContent();
        driver.findElement(By.cssSelector("body > div.header-box > div.user-box > div > span")).click();
    }
}
